# 导入Flask框架中的Blueprint, render_template, request, jsonify模块
from zipfile import error

from Tools.scripts.generate_opcode_h import header

from flask import Blueprint, render_template, request, jsonify, session

import os  # 导入os模块，用于文件和目录操作
from sqlalchemy.util.preloaded import sql_naming
from utils.database_util import query_data
# 创建一个名为article_publish的蓝图对象
article_publish = Blueprint('article_publish', __name__)
# def index():
#     return render_template('article_publish.html')
@article_publish.route('/publish')
def publish():
    return render_template('article_publish.html')

@article_publish.route('/add_article', methods=['GET', 'POST'])
def add_article():
    userid = session.get('userid')
    if userid is None:
        return "error"
    headline = request.form.get('headline')
    content = request.form.get('content')
    type = int(request.form.get('type'))
    credit = int(request.form.get('credit'))
    #drafted = int(request.form.get('drafted'))
    #checked = request.form.get('checked')
    #articleid = request.form.get('articleid')
    sql = "INSERT INTO article (userid , type , headline , content , credit) VALUES (%d, %d, %s, %s, %d)"%(userid, type, headline, content, credit)
    result = query_data(sql)
    return "OK"
    # return render_template('home.html')

# # 定义ueditor蓝图下的路由/uedit，支持GET和POST方法
# @article_publish.route('/uedit', methods=['GET', 'POST'])
# def uedit():
#     # 获取请求参数action的值
#     param = request.args.get('action')
#
#     # 如果请求方法是GET且action参数为'config'
#     if request.method == 'GET' and param == 'config':
#         # 返回config.json模板
#         return render_template('config.json')
#
#     # 如果请求方法是POST且action参数为'uploadimage'
#     elif request.method == 'POST' and request.args.get('action') == 'uploadimage':
#         # 从请求中获取上传的文件
#         f = request.files['upfile']
#         filename = f.filename  # 获取文件名
#         # 保存文件到指定路径
#         f.save('./resource/upload/' + filename)
#
#         # 构建返回结果字典
#         result = {}
#         result['state'] = 'SUCCESS'  # 设置状态为成功
#         result['url'] = f"/upload/{filename}"  # 设置文件访问URL
#         result['title'] = filename  # 设置文件标题
#         result['original'] = filename  # 设置原始文件名
#         return jsonify(result)  # 返回JSON格式的结果
#
#     # 如果请求方法是GET且action参数为'listimage'
#     elif request.method == 'GET' and param == 'listimage':
#         list = []  # 初始化列表用于存储图片信息
#         # 获取上传目录下的所有文件列表
#         filelist = os.listdir('./resource/upload')
#         # 遍历文件列表
#         for filename in filelist:
#             # 如果文件是png或jpg格式
#             if filename.lower().endswith('.png') or filename.lower().endswith('.jpg'):
#                 # 将文件信息添加到列表中
#                 list.append({'uri': '/upload/%s' % filename})
#
#         # 构建返回结果字典
#         result = {}
#         result['state'] = 'SUCCESS'  # 设置状态为成功
#         result['list'] = list  # 设置图片列表
#         result['start'] = 0  # 设置起始位置
#         result['total'] = 50  # 设置总数（假设值）
#         return jsonify(result)  # 返回JSON格式的结果
#
#
# # 定义article_publish蓝图下的路由/article_publish，仅支持GET方法
# @article_publish.route('/article_publish')
# def publish():
#     # 渲染并返回article_publish.html模板
#     return render_template('article_publish.html')